package org.example;

import java.util.List;
import java.util.Scanner;

/**
 * 习题集系统：支持加法、减法、混合题型，可生成50道题并验证答案
 */
public class ExerciseSystem {
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("===== 小学生数学习题系统 =====");
        // 扩展：增加混合题型选择
        System.out.println("请选择习题类型：1-加法  2-减法  3-加减法混合");
        int type = scanner.nextInt();

        // 固定支持50道题，也可保留动态输入（这里按需求固定为50）
        int count = 50;
        System.out.println("即将生成" + count + "道题...");

        // 创建对应类型的习题
        Exercise exercise;
        switch (type) {
            case 1:
                exercise = new Exercise("50道加法练习题");
                exercise.generateAddExercises(count);
                break;
            case 2:
                exercise = new Exercise("50道减法练习题");
                exercise.generateSubtractExercises(count);
                break;
            case 3:
                exercise = new Exercise("50道加减法混合练习题");
                exercise.generateMixedExercises(count);
                break;
            default:
                System.out.println("无效的题型类型！");
                return;
        }

        // 显示习题
        exercise.printExercises();

        // 验证答案（可选：批量答题或跳过）
        System.out.println("是否需要答题？(1-是 0-否)");
        if (scanner.nextInt() == 1) {
            checkAnswers(exercise.getExpressions());
        } else {
            System.out.println("程序结束！");
        }
    }

    // 保留原有答案验证逻辑
    private static void checkAnswers(List<AbstractExpression> expressions) {
        int correctCount = 0;
        System.out.println("\n请输入答案（按题目序号依次输入）：");

        for (int i = 0; i < expressions.size(); i++) {
            AbstractExpression exp = expressions.get(i);
            System.out.print("第" + (i + 1) + "题答案：");
            double userAnswer = scanner.nextDouble();

            if (exp.checkAnswer(userAnswer)) {
                correctCount++;
                System.out.println("正确！");
            } else {
                System.out.println("错误！正确答案是：" + exp.getResult());
            }
        }

        // 计算正确率
        double accuracy = (double) correctCount / expressions.size() * 100;
        System.out.println("\n===== 答题结束 =====");
        System.out.println("总题数：" + expressions.size() + " 题");
        System.out.println("做对：" + correctCount + " 题");
        System.out.println("正确率：" + String.format("%.1f", accuracy) + "%");
    }
}